/**
 * sendtask相关接口
 * Created by dongchen on 2022-03-22.
 */
const router = require('koa-router')();
const _ = require('lodash');

const httpResult = require('../lib/httpResult');

router.post('/', httpResult.resp(async ctx => {
    let data = ctx.request.body;
    let admin = ctx.session.admin;

    ctx.assert(data.template, 'param required : template');
    let template = await models.messageTemplate.findOne({_id : data.template});
    ctx.assert(template, 'template id error : not exist');

    let task = {
        name : data.name || undefined,
        template : template,
        template_name : template.name,
        template_type : template.type,
        mobiles : data.mobiles, //{type, [file or contact or input], total}
        send_type : data.send_type,
        send_time : data.send_time,
        from : data.from,
        creator : admin._id,
        customer_chain : admin.customer_chain,
        status : constant.TASK_STATUS.ACTIVATED,//现阶段直接激活
        create_time : Date.now()
    };

    return await models.sendTask.create(task);
}));

module.exports = router;
